package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.po.YgProductActionGift;
import com.ygqh.baby.service.YgProductActionGiftService;
import com.ygqh.baby.shiro.utils.SessionUtil;
import com.ygqh.baby.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/actiongift")
public class ActionGiftController extends BaseController<Object, java.lang.Long> {
    @Autowired
    private YgProductActionGiftService ygProductActionGiftService;

    @RequestMapping("save")
    @ResponseBody
    public Message save(String actionGift) {
        AdminUser user = SessionUtil.getCurrentUser();
        YgProductActionGift productActionGift = JsonUtils.toObject(actionGift, YgProductActionGift.class);
        if (productActionGift.getId() != null) {
            productActionGift.setUpdateTime(new Date());
            productActionGift.setUpdateBy(user.getTrueName());
        } else {
            productActionGift.setCreateBy(user.getTrueName());
            productActionGift.setCreateTime(new Date());
        }

        return ygProductActionGiftService.saveOrUpdate(productActionGift) == 1 ? SUCCESS_MESSAGE : ERROR_MESSAGE;
    }

    @RequestMapping("findById")
    @ResponseBody
    public Message findById(Long id) {
        YgProductActionGift gift = ygProductActionGiftService.getDetail(id);

        return Message.success(gift);
    }

    @RequestMapping("getProductAction")
    @ResponseBody
    public Message getProductAction(Long id) {
        List<YgProduct> list = ygProductActionGiftService.findActionProduct(id);
        return Message.success(list);
    }

    @RequestMapping("search")
    @ResponseBody
    public Map<String, Object> search(String q, QueryInfo queryInfo, Date startDate, Date endDate) {
        ResultSet<YgProductActionGift> resultSet = ygProductActionGiftService.search(queryInfo, q, startDate, endDate);
        return resultSet.toDatatables(null);
    }

    @RequestMapping("getProductActionGift")
    @ResponseBody
    public Message getProductActionGift(Long id) {
        List<YgProduct> list = ygProductActionGiftService.findProductGift(id);
        return Message.success(list);
    }

}
